<?php
ini_set('display_errors', 'off');
if (!$_POST)
    die('Wrong entry point!!!');

@extract($_REQUEST);
define('JPATH_BASE', dirname(__FILE__));
define('DS', DIRECTORY_SEPARATOR);
define('_JEXEC', 1);
require_once(JPATH_BASE . DS . "include" . DS . "arrays.php");
require_once(JPATH_BASE . DS . "include" . DS . "setup.php");
require_once(JPATH_BASE . DS . "include" . DS . "functions.php");

$period_from = $_POST['period_from'];
$period_to = $_POST['period_to'];
echo $from_to = "Period From: ". $period_from . " Period To: " . $period_to;
$period_from_file = date("dmY", strtotime($period_from));
$period_to_file = date("dmY", strtotime($period_to));

header("Pragma: no-cache");
header("Expires: 0");
header("Content-Type: application/vnd.ms-excel");
header("Content-disposition: attachment; filename=daily_report_".$period_from_file."_".$period_to_file.".xls");


$sql = "select id, companyname,surcharge,insurance_charge from companies where status = 1 order by companyname";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $company_arr[$row['id']] = $row['companyname'];
//    $a = $row['surcharge'];
//    $b = $row['insurance_charge'];
//    $totalCharge_arr[$row['id']] = $a + $b;
}

$sql = "select * from destinations where status = 1 order by destination_type, destination";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
    $destination_arr[$destination_type_arr[$row['destination_type']]][$row['id']] = $row['destination'];
}
?>

<?php
echo "<hr />";
$table = "inventories";
$cols = "id, date_format(booking_date ,'%d-%m-%Y') as `Date`, company_id as `Company`, cn_no, (select destination from destinations where id = destination_id) as `Destination`, weight, document_id as `Type`, total_charge as `freight charges`";
$where = "where booking_date between '".date("Y-m-d", strtotime($period_from))."' and '".date("Y-m-d", strtotime($period_to))."'";
$orderby = ($orderby == '') ? "id" : $orderby;
$sort = ($sort == '') ? "desc" : $sort;
$pagelength = 100;
$reparr = array();
if (is_array($company_arr)) {
    foreach ($company_arr as $k => $v) {
        $reparr['Company'][$k] = $v;
    }
}
foreach ($slab_type_arr as $k => $v) {
    $reparr['Type'][$k] = $v;
}
foreach ($weight_unit_arr as $k => $v) {
    $reparr['weight_unit'][$k] = $v;
}
foreach ($status_arr as $k => $v) {
    $reparr['status'][$k] = $v;
}
//if (is_array($totalCharge_arr)) {
//    foreach ($totalCharge_arr as $k => $v) {
//        $reparr['Total Charge'][$k] = $v;
//    }
//}
//print_r($reparr);
gettable("$table", $cols, "$where", "$orderby", "$sort", $pg, "$action", Array("id"), $reparr);

// echo  'sn' ."\t". 'booking_date' ."\t". 'cname' ."\t". 'cn_no' ."\t". 'dest' ."\t". 'weight' ."\t". 'destty' ."\t". 'total_charge' ."\n";
//$sql = "select c.id cid,c.companyname cname,d.id did,d.destination dest,d.destination_type destty,
//    d.status stt,i.* from companies c, destinations d, inventories i where c.id=i.company_id and d.id=i.destination_id and
//    i.booking_date between '".db_date_format($period_from)."' and '".db_date_format($period_to)."'";
//$result = mysql_query($sql);
//$i = 0;
//while ($row = mysql_fetch_array($result)) {
//    $i++;
//    $company_id = $row['cid'];
//    $company_name = $row['cname'];
//    $dest_id = $row['cid'];
//    $destination = $row['dest'];
//    $dest_type = $row['destty'];
//    //$dest_type = $destination_arr[$destination_type_arr[$row['destty']]];
//    $booking_date = $row['booking_date'];
//    $cn_no = $row['cn_no'];
//    $weight = $row['weight'];
//    $total_charge = $row['total_charge'];
//    echo $i." \t".$booking_date." \t".$company_name." \t".$cn_no." \t".$destination." \t".$weight." \t".$dest_type." \t".$total_charge."\n";
//}
?>